package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Dictionary;
import cn.wolfcode.crm.service.IDictionaryService;
import cn.wolfcode.crm.util.JSONResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("dictionary")
public class DictionaryController {

    @Autowired
    IDictionaryService dictionaryService;

    @RequestMapping("")
    @RequiresPermissions(value = {"字典目录首页","dictionary:index"} ,logical = Logical.OR)
    public String index(){

        return "dictionary/list";
    }

    @RequestMapping("list")
    @RequiresPermissions(value = {"字典目录列表","dictionary:list"} ,logical = Logical.OR)
    @ResponseBody
    public Object list(){
        return dictionaryService.list();
    }


    @RequestMapping("listData")
    @ResponseBody
    public Object listData(){
        return dictionaryService.list();
    }

    @RequestMapping("save")
    @RequiresPermissions(value = {"字典目录保存","dictionary:save"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult save(Dictionary entity){
        JSONResult result = new JSONResult();
        try{
            dictionaryService.save(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @RequiresPermissions(value = {"字典目录更新","dictionary:update"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult update(Dictionary entity){
        JSONResult result = new JSONResult();
        try{
            dictionaryService.update(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }


    @RequestMapping("delete")
    @RequiresPermissions(value = {"字典目录删除","dictionary:delete"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();
        try{
            dictionaryService.delete(id);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }
}
